package com.cb.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.mapper.BookingMapper;
import com.cb.mapper.CoordinateMapper;
import com.cb.mapper.UserEcardsMapper;
import com.cb.service.BookingService;
import com.jia.entity.Booking;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author JIASC
 * @description 针对表【t_booking】的数据库操作Service实现
 * @createDate 2024-05-08 21:09:56
 */
@Service
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking>
        implements BookingService {

    @Autowired
    private BookingMapper bookingMapper;

    @Autowired
    private CoordinateMapper coordinateMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public boolean addBooking(Booking booking) {
        int insert = bookingMapper.insert(booking);
        rabbitTemplate.convertAndSend("booking_exchange",
                "booking_routing_key",
                JSON.toJSONString(booking));
        Integer v = booking.getV();
        if (v == 1) {
            System.out.println("更新成功"+booking.getId());
            coordinateMapper.updatef(booking.getCid());
        } else {
            coordinateMapper.updates(booking.getCid());
        }

        return insert == 1;
    }

    @Autowired
    private UserEcardsMapper userEcardsMapper;

    @Override
    public boolean updateState(Integer uid, Integer eid, Integer cid) {
        int i = userEcardsMapper.updatecount(eid, uid);
        Booking booking = bookingMapper.findByUidAndEid(uid, cid);
        if (booking != null) {
            booking.setState(1);
            bookingMapper.updateById(booking);
        }
        return i == 1;
    }

    @Override
    public boolean checkhasbooking(Integer uid, Integer cid) {
        Booking booking = bookingMapper.sel(uid,cid);
        if (booking != null)return false;
        return true;
    }
}




